package com.niuxiaofa.newlaw.Tools;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by gxjkz on 2018/1/31.
 */

public class DBHelpter extends SQLiteOpenHelper {

    //***数据库名称
    private static final String DATABASE_NAME = "z_android_day14.db";
    //数据库版本号
    private static final int DATABASE_VERSION = 6;
    //创建表,用户信息表
    public static final String TABLE_USER_INFO = "user_info";

    public static final String TABLE_TASK = "task";

    public static final String TABLE_ROUTE = "route";

    public static final String TABLE_NOTIFICATION_MESSAGE = "notification";


    private static final String CREATE_USER_SQL = "CREATE TABLE "
            + TABLE_USER_INFO
            + " (_id Integer primary key autoincrement,"
            + " sex text,"
            + " username text,"
            + " password text);";


    private static final String CREATE_TASK_SQL = "CREATE TABLE "
            + TABLE_TASK
            + " (_id Integer primary key autoincrement,"
            + " task_name text,"
            + " task_describe text,"
            + " map_name text,"
            + " create_id text,"
            + " create_time datetime "
            + ");";

    private static final String CREATE_ROUTE_SQL = "CREATE TABLE "
            + TABLE_ROUTE
            + " (_id Integer primary key autoincrement,"
            + " name text,"
            + " task_id text,"
            + " type text"
            + ");";


    private static final String CREATE_NOTIFICATION_MESSAGE_SQL = "CREATE TABLE "
            + TABLE_NOTIFICATION_MESSAGE
            + " (_id Integer primary key autoincrement,"
            + " type text,"
            + " dataId text,"
            + " json text,"
            + " read text,"
            + " userId text"
            + ");";


    DBHelpter(Context context) {

        this(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    public DBHelpter(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public DBHelpter(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
//        db.execSQL(CREATE_USER_SQL);
//        db.execSQL(CREATE_TASK_SQL);
//        db.execSQL(CREATE_ROUTE_SQL);
        db.execSQL(CREATE_NOTIFICATION_MESSAGE_SQL);



    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTIFICATION_MESSAGE);
            onCreate(db);

        }
    }
}
